package com.lml.redis;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @date 2021/6/3 15:49
 * Description:
 */
public class TestRedisson2 {
    public static void main(String[] args) throws InterruptedException {

        new TestRedisson2().lock();
    }

    public Redisson configRedisson() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://localhost:6379").setDatabase(0);
        //设置看门狗的时间，不配置的话默认30000
        config.setLockWatchdogTimeout(30_000);
        return (Redisson) Redisson.create(config);
    }

    public void lock() throws InterruptedException {
        RLock redissonLock = configRedisson().getLock("watchDog");
        redissonLock.lock();
        try {
            //业务代码
            System.out.println("业务开始...");
            TimeUnit.SECONDS.sleep(21);
            System.out.println("业务结束...");
        } catch (Exception e) {
            System.out.println("获取锁异常");
        } finally {
            redissonLock.unlock();
        }
    }
}
